package com.fanshuai.spring.boot.component.service;

import com.fanshuai.spring.boot.component.handler.MessageHandler;
import com.fanshuai.spring.boot.domain.BookMsg;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class BookMessageHandler implements MessageHandler {
    private ObjectMapper mapper;

    public BookMessageHandler() {
        mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    public void handle(String topic, String message) {
        try {
            BookMsg msg = mapper.readValue(message, BookMsg.class);
            System.out.println(msg);
        } catch (Exception e) {
            log.error("handle error, ", e);
        }
    }

    public List<String> topics() {
        return ImmutableList.of("book-test");
    }
}
